package nl.alwinzwets.controllers
{
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.net.URLRequest;
	
	import nl.alwinzwets.events.GVEvents;
	import nl.alwinzwets.visuals.Blad;
	import nl.alwinzwets.visuals.Bloem;
	import nl.alwinzwets.visuals.Cirkel;
	import nl.alwinzwets.visuals.DNA;
	
	public class Glas extends MovieClip
	{
		private var _plant:Plant;
		private var _dna:DNA;
		private var _bloem:Bloem;
		private var _blad:Blad;
		private var _masker:Cirkel;
		private var _loader:Loader;
		private var _url:URLRequest;
		
		private var _dnapct:Number;
		
		private var _stngl:int;
		private var _bld:int;
		private var _blm:int;
		private var _mldrd:int;
		private var _stmpr:int;
		
		public var faded:int = 0;
		public var owner:int;
		
		public function Glas(stngl:int, bld:int, blm:int, mldrd:int, stmpr:int, ownr:int)
		{
			owner = ownr;
			_stngl = stngl;
			_bld = bld;
			_blm = blm;
			_mldrd = mldrd;
			_stmpr = stmpr;
			
			berekenDNA();
			
			// Start loading assets
			_url = new URLRequest("../assets/assets.swf");
			_loader = new Loader();			
			_loader.load(_url);
			
			// Start loaderHandler on load complete
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, startGlas);
			
		}
		
		private function berekenDNA():void
		{
			
			var stengelDNA:Number = Math.abs(GV.eigenStengel-_stngl)/6;
			var bladDNA:Number = Math.abs(GV.eigenBlad-_bld)/19;
			var bloemDNA:Number = Math.abs(GV.eigenBloem-_blm)/18;
			var meeldraadDNA:Number = Math.abs(GV.eigenMeeldraad-_mldrd)/9;
			var stamperDNA:Number = Math.abs(GV.eigenStamper-_stmpr)/6;
			
			var gemiddelde:Number = (stengelDNA+bladDNA+bloemDNA+((meeldraadDNA+stamperDNA) / 2)) / 4;
			_dnapct = Math.round((1-gemiddelde)*100);
		}
		
		private function startGlas(e:Event):void
		{
			_plant = new Plant(_loader,_stngl,_bld,_blm,_mldrd,_stmpr);
			addChild(_plant);
			
			_dna = new DNA();
			addChild(_dna);
			_dna.x = -50;
			_dna.y = -50;
			_dna.pctval.text = _dnapct + "%";
			_dna.visible = false;
			
			addBloem();
			addBlad();
				
			_masker = new Cirkel();
			addChild(_masker);
				
			_plant.addEventListener(GVEvents.PLANT_COMPLETE, onPlantComplete);
			
			_masker.x = _plant.x;
			_masker.y = _plant.y;
			
			_plant.mask = _masker;
			
		}
		
		private function addBloem():void
		{
			
    		var loadedBloem:Object = _loader.contentLoaderInfo.applicationDomain.getDefinition("Bloem"+_blm);
    		var mcBloem:MovieClip = new loadedBloem as MovieClip;
    		
    		// Assign meeldraad
    		var meelNr:String = "Meeldraad"+_mldrd+"_front";
			var loadedMeel:Object = _loader.contentLoaderInfo.applicationDomain.getDefinition(meelNr);
    		var mcMeel:MovieClip = new loadedMeel as MovieClip;
    		
    		// Assign meeldraad
    		var meelNrA:String = "Meeldraad"+_mldrd+"_back";
			var loadedMeelA:Object = _loader.contentLoaderInfo.applicationDomain.getDefinition(meelNrA);
    		var mcMeelA:MovieClip = new loadedMeelA as MovieClip;
    		
    		// Assign meeldraad
    		var stampNr:String = "Stamper"+_stmpr;
			var loadedStamp:Object = _loader.contentLoaderInfo.applicationDomain.getDefinition(stampNr);
    		var mcStamp:MovieClip = new loadedStamp as MovieClip;
			
			
			_bloem = new Bloem(mcBloem, mcMeel, mcMeelA, mcStamp);
			addChild(_bloem);
			_bloem.visible = false;
			_bloem.scaleX = 4; 
			_bloem.scaleY = 4; 
			_bloem.y = 110;
		}
		
		private function addBlad():void
		{	
			// Get random Bloem MC
    		var loadedBlad:Object = _loader.contentLoaderInfo.applicationDomain.getDefinition("Blad"+_bld);
    		var mcBlad:MovieClip = new loadedBlad as MovieClip;
			
			_blad = new Blad(mcBlad);
			addChild(_blad);
			_blad.visible = false;
			_blad.y = 110;
			_blad.scaleX = 4;
			_blad.scaleY = 4;
			
		}
		
		public function setMethod(method:String):void
		{
			switch(method){
				case "DNA":
					_plant.visible = false;
					_dna.visible = true;
					_blad.visible = false;
					_bloem.visible = false;
					_dna.mask = _masker;
					_dna.gotoAndPlay(1);
					break;
				case "Bloem":
					_plant.visible = false;
					_dna.visible = false;
					_blad.visible = false;
					_bloem.visible = true;
					_blad.mask = _masker;
					break;
				case "Blad":
					_plant.visible = false;
					_dna.visible = false;
					_blad.visible = true;
					_bloem.visible = false;
					_bloem.mask = _masker;
					break;
				case "Reset":
					_plant.visible = true;
					_dna.visible = false;
					_blad.visible = false;
					_bloem.visible = false;
					_plant.mask = _masker;
					break;
			}
		}
		
		private function onPlantComplete(e:Event):void{
			
		}

	}
}